package com.gs.common.utils;

import com.gs.common.utils.token.AccToken;
import com.gs.common.utils.token.JwtTokenUtils;

import javax.servlet.http.HttpServletRequest;

public class CommonUtils {

    private static final String DEFAULT_HEADER = "Authorization";

    private static final int DEFAULT_STRIP_LENGTH = 7;

    public  static  Long  getDefaultWebUserId(HttpServletRequest request){
        return  getUserId(request,DEFAULT_HEADER,DEFAULT_STRIP_LENGTH);
    }

    private static Long getUserId(HttpServletRequest request, String header, int stripLength) {
        String authorization = request.getHeader(header);
        String jwt = authorization.substring(stripLength);
        AccToken accToken = JwtTokenUtils.parseWebToken(jwt);
        Long userId = accToken.getUserId().longValue();
        return userId;

    }


}
